package com.akk.action;

import com.akk.business.LoginBusiness;
import com.akk.config.annotation.IgnoreAuth;
import com.akk.exception.RestBusinessException;
import com.akk.module.user.model.request.ForgetLoginPwdRequest;
import com.akk.module.user.model.request.UpdateLoginPwdRequest;
import com.akk.module.user.model.request.UserLoginRequest;
import com.akk.module.user.model.request.UserRegisterRequest;
import com.akk.module.user.model.response.UserLoginResponse;
import com.akk.utils.Constants;
import com.akk.utils.RestResponse;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.MediaType;
import org.springframework.web.bind.annotation.*;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpSession;
import javax.validation.Valid;
import java.util.UUID;

@RestController
@RequestMapping("/app")
@Api(value = "app-login",description = "用户登录相关信息接口")
public class LoginAction {

    @Autowired
    private LoginBusiness loginBusiness;

    @IgnoreAuth
    @PostMapping(value = "/login",produces = MediaType.APPLICATION_JSON_VALUE)
    @ApiOperation(value="用户登录",notes="用户通过手机号登录",httpMethod = "POST",response = RestResponse.class)
    public RestResponse userLogin(@ApiParam(required = true, value = "登录信息")@RequestBody @Valid UserLoginRequest request, HttpServletRequest servletRequest ){
        UserLoginResponse userLoginResponse = loginBusiness.userLogin(request);
        HttpSession session = servletRequest.getSession();
        session.setMaxInactiveInterval(60*30);
        session.setAttribute(Constants.WEB_USER_TOKEN_KEY,UUID.randomUUID()+"&&"+userLoginResponse.getUserId());
        return RestResponse.build(userLoginResponse);
    }


    @IgnoreAuth
    @PostMapping(value = "/register",produces = MediaType.APPLICATION_JSON_VALUE)
    @ApiOperation(value="用户注册",notes = "用户注册帐号",httpMethod = "POST",response = boolean.class)
    public boolean userRegister(@ApiParam(required = true,value = "注册信息")@RequestBody UserRegisterRequest request,
                                             HttpServletRequest servletRequest){
        return loginBusiness.userRegister(request,servletRequest);
    }

    @IgnoreAuth
    @RequestMapping(value = "/smsCode")
    @ApiOperation(value="发送短信验证码",notes="发送短信验证码",httpMethod = "GET",response = boolean.class)
    public boolean smsCode(@ApiParam(required = true,value = "手机号")@RequestParam(value = "phone") String phone,
                     HttpServletRequest request) {
        return loginBusiness.smsCode(phone,request);
    }

    @IgnoreAuth
    @RequestMapping(value = "/checkSmsCode")
    @ApiOperation(value="校验短信验证码",notes="校验短信验证码",httpMethod = "GET",response = boolean.class)
    public boolean checkSmsCode(@ApiParam(required = true,value = "手机号")@RequestParam(value = "phone")@Valid String phone,
                          @ApiParam(required = true,value = "验证码")@RequestParam(value = "code")@Valid String code,
                          HttpServletRequest request){
        String sessionCode = (String) request.getSession().getAttribute(phone);
        if (sessionCode != null && code.equals(sessionCode)){
            request.getSession().setAttribute(phone,"success");
            request.getSession().removeAttribute(Constants.SMS_CODE_TIMEOUT);
            return true;
        }
        throw new RestBusinessException("验证失败");
    }

    @RequestMapping(value = "/updateLoginPwd",method = RequestMethod.POST,produces = MediaType.APPLICATION_JSON_VALUE)
    @ApiOperation(value="修改登录密码",notes="修改登录密码",httpMethod = "POST",response = boolean.class)
    public boolean updateLoginPwd(@ApiParam(required = true,value = "修改密码")@RequestBody UpdateLoginPwdRequest request){
        return loginBusiness.updateLoginPwd(request);
    }

    @IgnoreAuth
    @RequestMapping(value = "/forgetPwd",method = RequestMethod.POST)
    @ApiOperation(value = "忘记登录密码",notes="忘记登录密码",httpMethod = "POST",response = boolean.class)
    public boolean forgetPwd(@ApiParam(required = true,value = "修改密码")@RequestBody ForgetLoginPwdRequest request,
                       HttpServletRequest servletRequest){
        return loginBusiness.forgetPwd(request,servletRequest);
    }


}
